"""
@File    :   route_depend.py
@Time    :   2024/03/28 23:35:28
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.token.schemas.out import (
    AccessTokenCompleteOutSchema,
    DoubleTokenCompleteOutSchema,
)
from src.business.admin.system.oauth2.token.schemas.validate import (
    AccessTokenListSearchValidateSchema,
    RefreshTokenLogoutCreateValidateSchema,
)
from src.business.admin.system.oauth2.token.service import TokenService
from src.dependencies.no_introduction import (
    depend_get_master_db_session,
    depend_get_token_redis_pool,
)
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)

from .no_introduction import (
    GetRefreshTokenDependencies,
    GetTenantIdFromAccessTokenDependencies,
    ValidateAccessTokenTenantAndReturnDependencies,
)


async def route_depend_refresh_access_token(
    refresh_token: Annotated[str, Depends(GetRefreshTokenDependencies.from_body)],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
    token_redis_pool: Annotated[Redis, Depends(depend_get_token_redis_pool)],
) -> DoubleTokenCompleteOutSchema:
    double_token = await TokenService.refresh_access_token(
        db_session, token_redis_pool, refresh_token
    )
    return double_token


async def route_depend_list_access_token(
    search_schema: AccessTokenListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[AccessTokenCompleteOutSchema], int]:
    """查询角色列表的路由依赖

    Args:
        search_schema (Annotated[TenantListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        tenant_id: 当前token指向的租户ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[TenantCompleteOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[AccessTokenCompleteOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await TokenService.list_search_access_token(
        db_session, tenant_id, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_logout_self_double_token(
    access_token: Annotated[
        AccessTokenCompleteOutSchema,
        Depends(ValidateAccessTokenTenantAndReturnDependencies.from_header),
    ],
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """注销登录的路由依赖类"""
    # 从访问令牌获取其刷新令牌
    refresh_token_list = [
        access_token.refresh_token,
    ]
    # 注销双Token
    await TokenService.logout_double_tokens(db_session, tenant_id, refresh_token_list)


async def route_depend_force_logout_double_token(
    logout_schema: RefreshTokenLogoutCreateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """强退双Token的路由依赖类"""
    # 强退双Token
    await TokenService.logout_double_tokens(
        db_session, tenant_id, logout_schema.refresh_token_list
    )
